﻿using EasyAdmin.Application.Contracts.Systems.Menus;
using EasyAdmin.Application.Contracts.Systems.Menus.Dtos;
using EasyAdmin.Application.Systems;
using EasyCore.App;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace EasyAdmin.Web.Host.Controllers
{
    /// <summary>
    /// 菜单管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    //[Authorize]
    public class MenusController : ControllerBase
    {
        private readonly IMenuAppService _menuAppService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="menuAppService"></param>
        public MenusController(IMenuAppService menuAppService)
        {
            this._menuAppService = menuAppService;
        }

        /// <summary>
        /// 获得所有菜单信息（树型）
        /// </summary>
        /// <returns></returns>
        [HttpGet("all-menus")]
        public async Task<dynamic> GetAllMenus()
        {
            return await _menuAppService.GetAllMenus();
        }

        /// <summary>
        /// 获得菜单信息
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        [HttpGet("{menuId}")]
        public async Task<dynamic> Get([FromRoute] long menuId)
        {
            return await _menuAppService.GetMenuByMenuId(menuId);
        }

        /// <summary>
        /// 删除菜单信息
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        [HttpDelete("{menuId}")]
        public async Task Delete([FromRoute] long menuId)
        {
            await _menuAppService.DeleteMenu(menuId);
        }

        /// <summary>
        /// 创建菜单信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task Post([FromBody] CreateUpdateMenuInput input)
        {
            await _menuAppService.CreateMenu(input);
        }

        /// <summary>
        /// 修改菜单信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task Put([FromBody] CreateUpdateMenuInput input)
        {
            await _menuAppService.UpdateMenu(input);
        }

        /// <summary>
        /// 创建菜单操作按钮
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("menu-operations")]
        public async Task CreateMenuOperation([FromBody] CreateUpdateMenuOperation input)
        {
            await _menuAppService.CreateMenuOperation(input);
        }

        /// <summary>
        /// 更新菜单操作按钮
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("menu-operations")]
        public async Task UpdateMenuOperation([FromBody]CreateUpdateMenuOperation input)
        {
            await _menuAppService.UpdateMenuOperation(input);
        }

        /// <summary>
        /// 根据操作按钮唯一标识删除按钮信息
        /// </summary>
        /// <param name="menuOperationId"></param>
        /// <returns></returns>
        [HttpDelete(@"menu-operations/{menuOperationId}")]
        public async Task DeleteMenuOperation([FromRoute]long menuOperationId)
        {
            await _menuAppService.DeleteMenuOperation(menuOperationId);
        }

        /// <summary>
        /// 获得菜单的按钮列表
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        [HttpGet(@"menu-operations/{menuId}")]
        public async Task<dynamic> GetMenuOperations([FromRoute]long menuId)
        {
            return await _menuAppService.GetMenuOperations(menuId);
        }

        /// <summary>
        /// 角色绑定菜单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost(@"bind-role-menus")]
        public async Task RoleBindMenu([FromBody]RoleBindMenuInput input)
        {
            await _menuAppService.RoleBindMenu(input);
        }

        /// <summary>
        /// 获得角色绑定的菜单信息
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet("role-menus")]
        public async Task<List<RoleMenuOutput>> GetRoleMenus(long roleId)
        {
            return await _menuAppService.GetRoleMenus(roleId);
        }

        /// <summary>
        /// 获得当前用户的所有菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet("user-menus")]
        public async Task<dynamic> GetUserMenus()
        {
            return await _menuAppService.GetUserMenusByUserId(EasyApp.UserId);
        }
    }
}
